﻿@using Fluxor.Blazor.Web.Components;
@using SimpleIdServer.IdServer.UI;
@using SimpleIdServer.IdServer.Website.Models;
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.ClientStore;
@using SimpleIdServer.IdServer.Website.Stores.ScopeStore;
@using SimpleIdServer.IdServer.Website.Stores.UserStore;
@inherits FluxorComponent
@inject IState<UpdateScopeMapperState> updateScopeMapperState
@inject IDispatcher dispatcher

<!-- Name -->
<div>
    <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.Name</RadzenText>
    <RadzenTextBox Name="Name" @bind-Value="@UserScopeMapperForm.Name" Class="w-100" Disabled="@IsEditModeEnabled"></RadzenTextBox>
    <RadzenRequiredValidator Component="Name" Text="@Global.NameIsRequired"></RadzenRequiredValidator>
</div>
@if(Scope != null)
{
    if (Scope.Protocol == ScopeProtocols.OPENID)
    {
        <!-- Token Claim Name -->
        <div>
            <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.TokenClaimName</RadzenText>
            <RadzenTextBox Name="TokenClaimName" @bind-Value="@UserScopeMapperForm.TokenClaimName" Class="w-100"></RadzenTextBox>
            <RadzenRequiredValidator Component="TokenClaimName" Text="@Global.TokenClaimNameRequired"></RadzenRequiredValidator>
            <p class="text-muted">@Global.ClaimToInsertInTheToken</p>
        </div>
        <!-- Claim JSON Type -->
        <div>
            <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.ClaimJsonType</RadzenText>
            <RadzenDropDown Name="ClaimJsonType" Class="w-100"
                    Data=@jsonTypeInfos
                    TValue="TokenClaimJsonTypes"
                    @bind-Value=@UserScopeMapperForm.ClaimJsonType
                    TextProperty="Name" ValueProperty="Type" />
            <RadzenRequiredValidator Component="ClaimJsonType" Text="Claim JSON Type is required"></RadzenRequiredValidator>
        </div>
    }
    else
    {
        <!-- Token Claim Name -->
        <div>
            <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.SamlAttributeName</RadzenText>
            <RadzenTextBox Name="SAMLAttributeName" @bind-Value="@UserScopeMapperForm.SAMLAttributeName" Class="w-100"></RadzenTextBox>
            <RadzenRequiredValidator Component="SAMLAttributeName" Text="@Global.SamlAttributeNameRequired"></RadzenRequiredValidator>
        </div>
    }
}

@code {
    [Parameter]
    public BaseUserScopeMapperForm UserScopeMapperForm { get; set; } = null!;
    [Parameter]
    public Scope Scope { get; set; } = null!;
    [Parameter]
    public bool IsEditModeEnabled { get; set; } = false;

    record ClaimJSONTypeInfo
    {
        public string Name { get; set; } = null!;
        public TokenClaimJsonTypes Type { get; set; }
    }

    List<ClaimJSONTypeInfo> jsonTypeInfos = new List<ClaimJSONTypeInfo>
    {
        new ClaimJSONTypeInfo { Type = TokenClaimJsonTypes.STRING, Name = "String" },
        new ClaimJSONTypeInfo { Type = TokenClaimJsonTypes.BOOLEAN, Name = "Boolean" },
        new ClaimJSONTypeInfo { Type = TokenClaimJsonTypes.INT, Name = "Integer" },
        new ClaimJSONTypeInfo { Type = TokenClaimJsonTypes.LONG, Name = "Long" },
        new ClaimJSONTypeInfo { Type = TokenClaimJsonTypes.DATETIME, Name = "DateTime" },
        new ClaimJSONTypeInfo { Type = TokenClaimJsonTypes.JSON, Name = "JSON" }
    };
}